package com.neusoft.weekreport.component.bizService.impl;

import com.neusoft.weekreport.component.bizService.MenuBizService;
import com.neusoft.weekreport.component.entities.*;
import com.neusoft.weekreport.component.req.GetAddMenuInfoReq;
import com.neusoft.weekreport.component.req.GetMenuByUserIdReq;
import com.neusoft.weekreport.component.req.GetMenuInfoByMenuIdReq;
import com.neusoft.weekreport.component.resp.*;
import com.neusoft.weekreport.mapper.BgMenuMapper;
import com.neusoft.weekreport.mapper.BgRoleRefMenuMapper;
import com.neusoft.weekreport.mapper.BgUserRefRoleMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class MenuBizServiceImpl implements MenuBizService {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    BgMenuMapper bgMenuMapper;
    @Resource
    BgUserRefRoleMapper bgUserRefRoleMapper;
    @Resource
    BgRoleRefMenuMapper bgRoleRefMenuMapper;

    //根据userid查询导航栏菜单
    @Override
    public GetMenuByUserIdListResp getBgMenu(GetMenuByUserIdReq req) throws Exception {
        GetMenuByUserIdListResp resp = new GetMenuByUserIdListResp();

        //查询用户和角色关联表
        BgUserRefRoleExample bgUserRefRoleExample = new BgUserRefRoleExample();
        BgUserRefRoleExample.Criteria criteria = bgUserRefRoleExample.createCriteria();
        criteria.andUserIdEqualTo(req.getUserId());
        List<BgUserRefRole> bgUserRefRoles = bgUserRefRoleMapper.selectByExample(bgUserRefRoleExample);
        List<String> roleList = new ArrayList<>();
        for (BgUserRefRole bgUserRefRole : bgUserRefRoles) {
            roleList.add(bgUserRefRole.getRoleId());
        }

        //查询角色和菜单关联表
        BgRoleRefMenuExample bgRoleRefMenuExample = new BgRoleRefMenuExample();
        BgRoleRefMenuExample.Criteria criteria1 = bgRoleRefMenuExample.createCriteria();
        criteria1.andRoleIdIn(roleList);
        List<BgRoleRefMenu> bgRoleRefMenus = bgRoleRefMenuMapper.selectByExample(bgRoleRefMenuExample);
        List<String> menuList = new ArrayList<>();
        for (BgRoleRefMenu bgRoleRefMenu : bgRoleRefMenus) {
            menuList.add(bgRoleRefMenu.getMenuId());
        }

        //查询菜单表
        BgMenuExample bgMenuExample = new BgMenuExample();
        BgMenuExample.Criteria criteria2 = bgMenuExample.createCriteria();
        criteria2.andIdIn(menuList);
        List<BgMenu> bgMenus = bgMenuMapper.selectByExample(bgMenuExample);
        resp.setMenuInfos(bgMenus);

        return resp;
    }

    //查询所有菜单
    @Override
    public GetMenuInfoListResp getMenuInfoList(GetMenuInfoByMenuIdReq req) {
        GetMenuInfoListResp resp = new GetMenuInfoListResp();
        BgMenuExample bgMenuExample = new BgMenuExample();
        BgMenuExample.Criteria criteria = bgMenuExample.createCriteria();
        List<BgMenu> bgMenuList = bgMenuMapper.selectByExample(bgMenuExample);
        resp.setMenuInfos(bgMenuList);
        return resp;
    }
    //通过menuid 查询菜单信息
    @Override
    public GetMenuInfoByMenuIdResp getMenuInfoByMenuId(GetMenuInfoByMenuIdReq req) throws Exception {
        GetMenuInfoByMenuIdResp resp = new GetMenuInfoByMenuIdResp();
        BgMenu bgMenu = bgMenuMapper.selectByPrimaryKey(req.getId());
        resp.setBgMenu(bgMenu);
        return resp;
    }
    //添加菜单信息
    @Override
    public GetAddMenuInfoResp addMenuInfo(GetAddMenuInfoReq req) throws Exception {
        GetAddMenuInfoResp resp = new GetAddMenuInfoResp();
        String id = UUID.randomUUID().toString();
        BgMenu bgMenu = new BgMenu();
        bgMenu.setId(id);
        bgMenu.setName(req.getName());
        bgMenu.setOrderid(req.getOrder());
        bgMenu.setPath(req.getPath());
        bgMenu.setParentId(req.getParentId());
        bgMenu.setDescription(req.getDescription());
        bgMenu.setIcon(req.getIcon());
        bgMenu.setEnable(req.getEnable());
        bgMenu.setIfShow(req.getIfShow());
        bgMenu.setCreateDate(sysDateTime());
        bgMenu.setUpdateDate(sysDateTime());
        bgMenuMapper.insert(bgMenu);
        resp.setId(id);
        return resp;

    }
    //修改菜单信息
    @Override
    public void modifyMenuInfoByMenuId(GetAddMenuInfoReq req) throws Exception {
        BgMenu bgMenu= new BgMenu();
        bgMenu.setId(req.getId());
        bgMenu.setName(req.getName());
        bgMenu.setOrderid(req.getOrder());
        bgMenu.setPath(req.getPath());
        bgMenu.setParentId(req.getParentId());
        bgMenu.setDescription(req.getDescription());
        bgMenu.setIcon(req.getIcon());
        bgMenu.setEnable(req.getEnable());
        bgMenu.setIfShow(req.getIfShow());
        bgMenu.setUpdateDate(sysDateTime());
        bgMenuMapper.updateByPrimaryKeySelective(bgMenu);
    }

    //删除菜单
    @Override
    @Transactional
    public GetCommenResp deleteMenuInByMenuId(GetMenuInfoByMenuIdReq req) throws Exception {
        GetCommenResp resp = new GetCommenResp();
        //删除菜单
        Integer sum=bgMenuMapper.deleteByPrimaryKey(req.getId());
        if(sum==0){
            resp.setCode("-1");
            resp.setMsg("删除失败");
        }else{
            //删除角色和菜单关联表
            BgRoleRefMenuExample bgRoleRefMenuExample = new BgRoleRefMenuExample();
            BgRoleRefMenuExample.Criteria criteria = bgRoleRefMenuExample.createCriteria();
            criteria.andMenuIdEqualTo(req.getId());
            Integer bgRoleRefMenuSum = bgRoleRefMenuMapper.deleteByExample(bgRoleRefMenuExample);
            if(bgRoleRefMenuSum==0){
                resp.setCode("-1");
                resp.setMsg("删除失败");
            }

        }
        return resp;
    }

    private String sysDateTime(){
        Date date = new Date();
        DateFormat dataFormat = new SimpleDateFormat("yyy-MM-dd hh:mm:ss");
        String createDate = dataFormat.format(date);
        return createDate;
    }
}